window.writeDetail = new Vue({
    el: '#exerciseDetail',
    data() {
        return{
            second:0,//
            time:'00:00:00',//做题时长
            interVal:'',//
            isFull:false,//是否全屏
            writeText:'',//用户答案
            count:0,//总字数
            questionData:'',//题目数据
            content_id:'',//当前题目id
        }
    },
    created() {
        this.getQuestion();//获取做题数据
    },
    mounted() {
        let that = this;
        let clock = sessionStorage.getItem('writeTime'+$('#content_id').val());
        if(clock){
            that.time = that.formateSeconds(clock);
            that.second = clock;
        }
        that.$nextTick(function () {
            that.$refs.focusTextarea.focus();
            //做题时长
            that.interVal = setInterval(function () {
                that.second ++;
                that.time = that.formateSeconds(that.second);
                sessionStorage.setItem('writeTime'+$('#content_id').val(),that.second);
            },1000);
        });
    },
    methods: {
        getQuestion(){//获取题目
            let that = this;
            $.get('/cn/exercise/write-exercise',{
                content_id:$('#content_id').val(),
            },function (res) {
                if(res.code == 1){
                    that.content_id = $('#content_id').val();
                    that.questionData = res;
                }else {
                    alert(res.message)
                }
            },'json')
        },
        //  提交作文
        essayUp(){
            let that = this;
            $.post('/cn/exercise/save-spoken-write-answer',{
                testid:that.questionData.son.content_id,
                fid:that.questionData.son.cate_id,
                pid:that.questionData.son.pid,
                resultid:that.questionData.resultid,
                newid:that.questionData.newid,
                answer:that.writeText,
                typeid:'3',//3写作4口语
                time:that.second,
            },function (res) {
                if(res.code == 1){
                    window.location.href = '/cn/exercise/write-review?content_id='+ that.content_id;
                }else {
                    alert(res.message)
                }
            },'json')
        },
        //  统计英文单词数
        wordStatic(){
            let text = this.writeText.replace(/\r\n/g, " ").replace(/\n/g, " ").replace(/<[^>]+>/g,"");

            let arr = text.split(" ");
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].length < 1) {
                    arr.splice(i, 1);
                    i = i - 1;
                }
            }
            this.count = arr.length;
        },
        //  全屏、退出全屏
        launchFullscreen() {
            let that = this;
            let element = document.documentElement;
            if(!that.isFull){
                if(element.requestFullscreen) {
                    element.requestFullscreen();
                } else if(element.mozRequestFullScreen) {
                    element.mozRequestFullScreen();
                } else if(element.webkitRequestFullscreen) {
                    element.webkitRequestFullscreen();
                } else if(element.msRequestFullscreen) {
                    element.msRequestFullscreen();
                }
                that.isFull = true;
            }else {
                if(document.exitFullscreen) {
                    document.exitFullscreen();
                } else if(document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if(document.webkitExitFullscreen) {
                    document.webkitExitFullscreen();
                }
                that.isFull = false;
            }
        },
        //时间转换
        formateSeconds(endTime){
            let secondTime = parseInt(endTime);//将传入的秒的值转化为Number
            let min = 0;// 初始化分
            let h =0;// 初始化小时
            let result='';
            if(secondTime>60){//如果秒数大于60，将秒数转换成整数
                min=parseInt(secondTime/60);//获取分钟，除以60取整数，得到整数分钟
                secondTime=parseInt(secondTime%60);//获取秒数，秒数取佘，得到整数秒数
                if(min>60){//如果分钟大于60，将分钟转换成小时
                    h=parseInt(min/60);//获取小时，获取分钟除以60，得到整数小时
                    min=parseInt(min%60);//获取小时后取佘的分，获取分钟除以60取佘的分
                }
            }
            result=`${h.toString().padStart(2,'00')}:${min.toString().padStart(2,'00')}:${secondTime.toString().padStart(2,'00')}`;
            return result;
        }
    }
});